﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BillUI.AutoMapper;
using BLL;
using Model;

namespace BillUI.Controllers
{
    public class BillController : Controller
    {
        // GET: Bill
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(int id = 0)
        {
            BillTypeBLL bll = new BillTypeBLL();
            ViewBag.BillTypeList = bll.GetList();

            ViewBag.id = id;

            return View();
        }

        [HttpGet]
        public ActionResult GetBillList(int billType = 0,
            string beginTime = "", string endTime = "", int page = 1, int limit = 10)
        {
            int count = 0;  //总条数

            BillBLL bll = new BillBLL();

            List<Bills> bList = bll.GetBillListBLL(billType, beginTime, endTime, page, limit, out count);

            //
            var query = MapperTools.Mapper.Map<List<Bills_DTO>>(bList);



            var json = new
            {
                code = 0,
                msg = "",
                count = count,
                data = query
            };

            return Json(json, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public int SaveBillsInfo(Bills bills)
        {
            BillBLL bll = new BillBLL();
            return bll.SaveBillsInfoBLL(bills);
        }

        [HttpPost]
        public ActionResult GetBillssInfoById(int id = 0)
        {
            BillBLL bll = new BillBLL();

            Bills info = bll.GetBillssInfoByIdBLL(id);

            if (info == null)
                return null;

            var query = MapperTools.Mapper.Map<Bills_DTO>(info);

            return Json(query, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public int Delete(int id = 0)
        {
            BillBLL bll = new BillBLL();
            return bll.DeleteBLL(id);
        }


        [HttpPost]
        public int DeleteBatchBills(Bills[] bills)
        {
            if (bills == null || bills.Length == 0)
                return 0;

            BillBLL bll = new BillBLL();
            return bll.DeleteBatchBillsBLL(bills);
        }
    }
}